﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Nvigorate.Common.Reflection;
using Nvigorate.Extensions;

namespace Nvigorate.Test.Extensions
{
    [TestClass]
    public class IEnumerableExtendersTests
    {
        #region Denormalize Tests

        [TestMethod]
        public void Denormalize_StringArrayContainsNull_PreservesType()
        {
            // Arrange
            var subject = new [] {"test", null};

            // Act
            var actual = subject.Denormalize().ToArray();

            // Assert 
            Assert.AreEqual(2, actual.Length);
        }

        [TestMethod]
        public void Denormalize_StringArray_PreservesType()
        {
            // Arrange
            var subject = new[] {"test"};

            // Act
            var actual = subject.Denormalize().ToArray();

            // Assert 
            Assert.AreEqual(1, actual.Length);
            Assert.IsInstanceOfType(actual[0], typeof (string));
        }

        [TestMethod]
        public void Denormalize_JaggedStringArray_SingleArrayContainsAllItems()
        {
            // Arrange
            var subject = new[] {new[] {"1", "2"}, new[] {"3", "4"}};
            string[] expected = new[] {"1", "2", "3", "4"};

            // Act
            var actual = subject.Flatten().ToArray();

            // Assert
            Assert.AreEqual(4, actual.Length);
            Assert.IsTrue(expected.SequenceEqual(actual));
        }

        #endregion

        #region IEnumerableConverters

        [TestMethod]
        public void TestUniformConversionExtenders()
        {
            var test1 = "test1";
            var test2 = "test2";
            var test3 = "test3";

            var goodSource = new object[]
                                 {
                                     test1, test2, test3
                                 };

            var goodList = goodSource.Cast<string>();

            var testSet = new List<string>()
                              {
                                  test1,
                                  test2,
                                  test3
                              };
            Assert.IsTrue(testSet.SequenceEqual(goodList));
        }

        #endregion



    }
}